﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InputProductForm
{
    public partial class frmInputProduct :UTLBASEFORM.MDICHILD
    {
        TProductType tProdType = new TProductType();
        TCat tCat = new TCat();
        TProduct tProduct = new TProduct();
        InfoSeed infoSL = new InfoSeed();
        InfoSeed infoSM = new InfoSeed();
        InfoShell infoShell = new InfoShell();
        InfoProduct infoPr = new InfoProduct();
        DataTable dataSeedL;
        DataTable dataSeedS;
        DataTable dataCat;
        DataTable data;
        public frmInputProduct()
        {
            InitializeComponent();
        }

        private void frmInputProduct_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
            LoadProductType();
            LoadMaterial();
            LoadUnit();
            LoadAutoSeedL();
            LoadAutoSeedS();
        }

        private void SetupMenuBar()
        {
            ShowBTN1(TextLabelAdd, ImageNameCheck_Ok);
            ShowBTN2(TextLabelEdit, ImageNameEdit);
            ShowBTN3(TextLabelDelete, ImageNameDelete);
            ShowBTN4("Hủy", null);
        }

        public void LoadAutoSeedL() {
           dataSeedL= tProduct.SelectSeed("Hạt Lớn");
            AutoCompleteStringCollection collec = new AutoCompleteStringCollection();

            for (int i = 0; i < dataSeedL.Rows.Count; i++)
            {
                collec.Add(dataSeedL.Rows[i]["Size"].ToString());
            }
            txtWSeedL.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtWSeedL.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtWSeedL.AutoCompleteCustomSource = collec;
        }
        public void LoadAutoSeedS()
        {
            dataSeedS = tProduct.SelectSeed("Hạt Tấm");
            AutoCompleteStringCollection collec = new AutoCompleteStringCollection();

            for (int i = 0; i < dataSeedS.Rows.Count; i++)
            {
                collec.Add(dataSeedS.Rows[i]["Size"].ToString());
            }
            txtWSeedT.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtWSeedT.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtWSeedT.AutoCompleteCustomSource = collec;
        }

        public void LoadProductType() {
            DataTable data = tProdType.GetProType();
            cmbProductType.DisplayMember=data.Columns["Name"].ToString();
            cmbProductType.ValueMember=data.Columns["ID"].ToString();
            cmbProductType.DataSource = data;
            
        }
        public void LoadCat(int idType) {
            dataCat = tCat.GetCat(idType);
            cmdCat.DataSource = dataCat;
            cmdCat.DisplayMember = dataCat.Columns["Name"].ToString();
            cmdCat.ValueMember = dataCat.Columns["ID"].ToString();
        }
        public void LoadMaterial() {
            DataTable data = tProduct.SelectMetarial();
              cmdMaterial.DisplayMember=data.Columns["Name"].ToString();
            cmdMaterial.ValueMember=data.Columns["ID"].ToString();
            cmdMaterial.DataSource = data;
        }
        public void LoadUnit()
        {
            DataTable data = tProduct.SelectUnit();
            cmdUnit.DisplayMember = data.Columns["Name"].ToString();
            cmdUnit.ValueMember = data.Columns["ID"].ToString();
            cmdUnit.DataSource = data;
        }
        private void cmbProductType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCat(int.Parse(cmbProductType.SelectedValue.ToString()));
        }

        private void txtWSeedL_TextChanged(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < dataSeedL.Rows.Count; i++)
                if (dataSeedL.Rows[i]["Size"].ToString() == txtWSeedL.Text)
                    break;
            if (i == dataSeedL.Rows.Count)
            {
                txtPriceSeekL.Text = "";
                infoSL.Id = 0;
            }
            else
            {
                txtPriceSeekL.Text = dataSeedL.Rows[i]["Price"].ToString();
                infoSL.Id = int.Parse(dataSeedL.Rows[i]["id"].ToString());

            }
        }

        private void txtWSeedT_TextChanged(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < dataSeedS.Rows.Count; i++)
                if (dataSeedS.Rows[i]["Size"].ToString() == txtWSeedT.Text)
                    break;
            if (i == dataSeedS.Rows.Count)
            {
                txtPriceSeedT.Text = "";
                infoSM.Id = 0;
            }
            else
            {
                txtPriceSeedT.Text = dataSeedS.Rows[i]["Price"].ToString();
                infoSM.Id = int.Parse(dataSeedS.Rows[i]["id"].ToString());
            }
        }

        public void GetSeed() {

            infoSL.Name = "Hạt Lớn";
            infoSL.Size = txtWSeedL.Text;
            infoSL.Price = float.Parse(txtPriceSeekL.Text);
            infoSL.DatePrice = DateTime.Parse(dtpkSeedL.Text.ToString());
            infoSL.IDUnit = 0;
            ///
            infoSM.Name = "Hạt Tấm";
            infoSM.Size = txtWSeedT.Text;
            infoSM.Price = float.Parse(txtPriceSeedT.Text);
            infoSM.DatePrice = DateTime.Parse(dtpkSeedM.Text.ToString());
            infoSM.IDUnit = 0;
        }
        public void GetShell() {
            infoShell.Weight = int.Parse(txtWShell.Text);
            infoShell.Price = float.Parse(txtPriceShell.Text);
            infoShell.IDMaterial = int.Parse(cmdMaterial.SelectedValue.ToString());
            infoShell.UnitID = 0;
        }
        public void GetProduct() {
            infoPr.Name = cmdCat.Text;
            infoPr.IDCat =int.Parse(cmdCat.SelectedValue.ToString());
            infoPr.IDSeedLarge = infoSL.Id;
            infoPr.NumberSeedLarge =int.Parse(txtNumSeedL.Text);
            infoPr.IDSeedSmall = infoSM.Id;
            infoPr.NumberSeedSmall = int.Parse(txtNumSeedT.Text);
            infoPr.IDShell = infoShell.ID;
            infoPr.TienCong =float.Parse(txtTienCong.Text);
            infoPr.Statue = 0;
            infoPr.Barcode = SetBacode();
        }
        public string SetBacode() {
            string barcode = "";
            string code = tProduct.GetMaxID();
            if (code == DBNull.Value.ToString()) code = "0";
            int i;
            for (i = 0; i < dataCat.Rows.Count; i++)
                if (dataCat.Rows[i]["ID"].ToString() == cmdCat.SelectedValue.ToString())
                    break;
            string str=dataCat.Rows[i]["Symbol"].ToString();
           
                code =(int.Parse(code) + 1).ToString("00000000");
                if (str.Trim().Length == 1) str = str.Trim()+ "0";
                barcode = str.Trim() + code;
                return barcode;
        }
        public int InsertSe(InfoSeed info)
        {
            if (info.Id == 0)
                info.Id = tProduct.InsertSeed(info);
            else
            {
                tProduct.UpdateSeed(info);
            }
            return info.Id;
        }

        public int InsertSh() {
            infoShell.ID = tProduct.InsertShell(infoShell);
            return infoShell.ID;
        }

        public int InsertPr() {
            infoPr.ID = tProduct.InsertProduct(infoPr);
            return infoPr.ID;
        }

        public void LoadProduct() {
            lvProduct.Items.Clear();
            data = tProduct.LoadProductWithStatus(0);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                int stt = i + 1;
                ListViewItem item = new ListViewItem(stt.ToString());
                lvProduct.Items.Add(item);

                // item.SubItems.Add(stt.ToString());

               // item.SubItems.Add(data.Rows[i]["FullName"].ToString());
                if (i % 2 == 0)
                {
                    item.BackColor = Color.AliceBlue;
                    //return ;
                }
                else
                {
                    item.BackColor = Color.AntiqueWhite;
                    //return Brushes.Beige;
                }

            }
        }

        public override void btn1_Click(object sender, EventArgs e)
        {
            GetSeed();
            InsertSe(infoSL);
            InsertSe(infoSM);
            GetShell();
            InsertSh();
            GetProduct();
            InsertPr();
        }

    }
}